package net.huashitong.Indicators.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.dao.IndustryDao;
import net.huashitong.Indicators.entity.Industry;
import net.huashitong.Indicators.entity.ThreeIndex;
import net.huashitong.Indicators.filter.IndustryFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IndustryService extends StringPKService<Industry> {

    @Autowired
    private IndustryDao dao;

    @Autowired
    private SysTableService tabservice;

    @Override
    protected BaseDao<Industry, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(Industry obj) {
        obj.setSysId(tabservice.updateMaxSysId("CA01", null));
    }

    /**
     * 动态查询行业排名信息
     *
     * @param filter
     * @return
     */
    public List<Industry> queryIndustry(IndustryFilter filter) {

        return dao.queryIndustrys(filter);
    }

    public List<Industry> getCatalog() {
        return dao.getCatalog();
    }

    public Map<String, Object> getByIndustry(IndustryFilter filter) {
        List<Industry> list = dao.queryIndustrys(filter);
        List<Map<String, Object>> listmap = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        for (Industry item : list) {
            Map<String, Object> randata = new HashMap<>();
            int t = Integer.parseInt(item.getDataTime().substring(5));

            randata.put("datetime", item.getDataTime().substring(0, 4) + (t > 6 ? "下半年" : "上半年"));
            randata.put("rankData", item.getRankData());
            listmap.add(randata);
        }
        if (ObjectUtils.isNotBlank(list)) {

            map.put("industryCode", list.get(0).getIndustryCode());

            map.put("industryName", list.get(0).getIndustryName());
            map.put("title", list.get(0).getTitle());
            //  map.put("ranks",list.get(0).getRanks());
            map.put("companyName", list.get(0).getCompanyName());
            map.put("rankDataList", listmap);
            // map.put("dataTime",list.get(0).d)
            map.put("haveData", list.get(0).getHaveData());
        }


        return map;
    }
}
